﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Drawing;

namespace RCT2
{

    public class BarSettings : ICloneable
    {
        public int timer { get; set; }
        public int[] percents;
        public int percent0 {
            get { return percents[0]; }
            set { percents[0] = value; }
        }
        public int percent1
        {
            get { return percents[1]; }
            set { percents[1] = value; }
        }
        public Color[] colours { get; set; }
        public Color colour0
        {
            get { return colours[0]; }
            set { colours[0] = value; }
        }
        public Color colour1
        {
            get { return colours[1]; }
            set { colours[1] = value; }
        }
        public Color colour2
        {
            get { return colours[2]; }
            set { colours[2] = value; }
        }
        public int varIndex { get; set; }
        public bool leftAligned { get; set; }

        public BarSettings(int _timer, int[] _percents, Color[] _colours, int _varIndex, bool _leftAligned)
        {
            timer = _timer;
            percents = _percents;
            colours = _colours;
            varIndex = _varIndex;
            leftAligned = _leftAligned;
        }
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    public static class Settings
    {
        //Default bar settings

        public static int memType = 1;
        public static bool splitByHeight = true;
        public static bool OptionsAtStartup = false;
        public static bool autoUpdate = true;
        public static bool fade = true;
        public static bool HideIcon = false;
        public static int side = 0;
        public static DateTime lastUpdate = DateTime.Now.AddDays(-2.0d);

        public static BarSettings oneSettings = new BarSettings(1000, new int[2] { 50, 75 }, new Color[3] { Color.CadetBlue, Color.Orange, Color.Red }, 1, false);
        public static BarSettings twoSettings = new BarSettings(10000, new int[2] { 60, 80 }, new Color[3] { Color.LimeGreen, Color.Orange, Color.Red }, 0, false);

        static StreamWriter sw;
        static StreamReader sr;

        static Settings()
        {
            if (!File.Exists(Environment.CurrentDirectory + "\\settings.rcts"))
                writeSettings();
        }

        public static void writeSettings()
        {
            sw = new StreamWriter(Environment.CurrentDirectory + "\\settings.rcts");


            sw.WriteLine("oneIndex\t" + oneSettings.varIndex.ToString());
            sw.WriteLine("oneLeftAlign\t" + oneSettings.leftAligned.ToString());
            sw.WriteLine("oneTimer\t" + oneSettings.timer.ToString());
            sw.WriteLine("onePercents\t" + oneSettings.percents[0] + "\t" + oneSettings.percents[1]);
            sw.WriteLine("oneColours\t" + oneSettings.colours[0].ToArgb().ToString() + "\t" + oneSettings.colours[1].ToArgb().ToString() + "\t" + oneSettings.colours[2].ToArgb().ToString());

            sw.WriteLine("twoIndex\t" + twoSettings.varIndex.ToString());
            sw.WriteLine("twoLeftAlign\t" + twoSettings.leftAligned.ToString());
            sw.WriteLine("twoTimer\t" + twoSettings.timer.ToString());
            sw.WriteLine("twoPercents\t" + twoSettings.percents[0] + "\t" + twoSettings.percents[1]);
            sw.WriteLine("twoColours\t" + twoSettings.colours[0].ToArgb().ToString() + "\t" + twoSettings.colours[1].ToArgb().ToString() + "\t" + twoSettings.colours[2].ToArgb().ToString());
            
            sw.WriteLine("memType\t" + memType.ToString());
            
            sw.WriteLine("splitByHeight\t" + splitByHeight.ToString());
            sw.WriteLine("OptionsAtStartup\t" + OptionsAtStartup.ToString());
            sw.WriteLine("autoUpdate\t" + autoUpdate.ToString());
            sw.WriteLine("lastUpdate\t" + lastUpdate.ToShortDateString());
            sw.WriteLine("fade\t" + fade.ToString());
            sw.WriteLine("HideIcon\t" + HideIcon.ToString());
            sw.Flush();
            sw.Close();
        }

        public static void readSettings()
        {
            sr = new StreamReader(Environment.CurrentDirectory + "\\settings.rcts");
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine().Trim();
                if (line != "")
                {
                    string[] parts = line.Split('\t');

                    try
                    {
                        if (parts[0] == "twoColours")
                        {
                            twoSettings.colours[0] = Color.FromArgb(Convert.ToInt32(parts[1])); twoSettings.colours[1] = Color.FromArgb(Convert.ToInt32(parts[2])); twoSettings.colours[2] = Color.FromArgb(Convert.ToInt32(parts[3]));
                        }
                        else if (parts[0] == "oneColours")
                        {
                            oneSettings.colours[0] = Color.FromArgb(Convert.ToInt32(parts[1])); oneSettings.colours[1] = Color.FromArgb(Convert.ToInt32(parts[2])); oneSettings.colours[2] = Color.FromArgb(Convert.ToInt32(parts[3]));
                        }
                        else if (parts[0] == "oneTimer")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            if (a < 100 || a > 60000) throw new ArgumentOutOfRangeException();
                            oneSettings.timer = a;
                        }
                        else if (parts[0] == "twoTimer")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            if (a < 100 || a > 60000) throw new ArgumentOutOfRangeException();
                            twoSettings.timer = a;
                        }
                        else if (parts[0] == "memType")
                            memType = Convert.ToInt32(parts[1]);
                        else if (parts[0] == "onePercents")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            int b = Convert.ToInt32(parts[2]);
                            if (a > 100 || a < 1 || b > 100 || b < 1) throw new ArgumentOutOfRangeException();
                            oneSettings.percents[0] = a; oneSettings.percents[1] = b;
                        }
                        else if (parts[0] == "twoPercents")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            int b = Convert.ToInt32(parts[2]);
                            if (a > 100 || a < 1 || b > 100 || b < 1) throw new ArgumentOutOfRangeException();
                            twoSettings.percents[0] = a; twoSettings.percents[1] = b;
                        }
                        else if (parts[0] == "splitByHeight")
                            splitByHeight = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "oneLeftAlign")
                            oneSettings.leftAligned = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "twoLeftAlign")
                            twoSettings.leftAligned = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "oneIndex")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            if (a < 0 || a > 2) throw new ArgumentOutOfRangeException();
                            oneSettings.varIndex = a;
                        }
                        else if (parts[0] == "twoIndex")
                        {
                            int a = Convert.ToInt32(parts[1]);
                            if (a < 0 || a > 2) throw new ArgumentOutOfRangeException();
                            twoSettings.varIndex = a;
                        }
                        else if (parts[0] == "OptionsAtStartup")
                            OptionsAtStartup = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "autoUpdate")
                            autoUpdate = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "lastUpdate")
                            lastUpdate = DateTime.Parse(parts[1]);
                        else if (parts[0] == "fade")
                            fade = Convert.ToBoolean(parts[1]);
                        else if (parts[0] == "HideIcon")
                            HideIcon = Convert.ToBoolean(parts[1]);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error occurred when parsing the setting \"" + parts[0] + "\". The default value for this setting will be used.", "RCT2");
                        continue;
                    }
                }
            }
            sr.Close();
        }
    }
}
